<?php

namespace app\common\service;


use app\common\helper\FormatHelper;
use app\web\constant\CommonConstant;
use think\Db;
use util\Tree;

/**
 * 员工管理技师服务
 * Class EngineerService
 * @package app\common\service
 */
class EngineerService {

    /**
     * 通过ID获取技师数据
     *
     * @param int $id 技师ID
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getEnineerById(int $id,$store_id) {
        $data = self::getEnineerByIds([$id],$store_id);
        return $data[0] ?? [];
    }


    /**
     * 通过多个ID获取技师数据
     *
     * @param int $ids
     * @param int $store_id
     * @param int $status
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getEnineerByIds (array $ids ,int $store_id) {

        $data = Db::name('qt_users')
            ->whereIn('id',$ids)
            ->where(['store_id' => $store_id])
            ->select();

        return $data;
    }


    /**
     * 根据手牌号码更新手牌表员工ID
     *
     * @param $hand_code
     * @param $userId
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function updateHandCard($hand_code,$userId) {

        $card_data = Db::name('qt_hand_card')
            ->alias('ca')
            ->field('gr.id')
            ->leftJoin('qt_hand_grant gr','ca.id = gr.hand_card_id')
            ->where('ca.status','<>',3)
            ->where('ca.pass',$hand_code)
            ->where('gr.type='.CommonConstant::HAND_GRANT_TYPE_STAFF)
            ->find();
        if (!empty($card_data)) {
            Db::name('qt_hand_grant')->update(['id'=>$card_data['id'],'uv_id'=>$userId]);
        }

    }


    /**
     * 技师离职校验逻辑
     */
    public static function checkEngineerStatus($engineer_id){

        $engineer_data = Db::name('qt_users')
            ->where('id',$engineer_id)
            ->whereIn('work_status',[1,3,4])
            ->find();
        if ($engineer_data) {
            return FormatHelper::fReturn('技师离职前必须激活技师并无上班,无预约，无服务中','',FormatHelper::ERROR);
        }
    }


    /**
     * 插入菜单权限
     * @param $user_id   用户ID
     * @param $role_id   角色ID
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function insertUserMenuAuth($user_id,$role_id) {

        $role_menu_data = Db::name('qt_user_role_menu')->alias('role')
            ->field([
                'role.platform',
                'menu.id',
                'menu.title',
                'menu.pid',
            ])
            ->leftJoin('qt_user_menu menu','role.menu_id=menu.id')
            ->where('role.role_id',$role_id)
            ->select();
        if (!empty($role_menu_data)) {

            $menu_auth = Db::name('qt_user_menu_auth')->where('user_id',$user_id)->find();
            if (!empty($menu_auth)) {
                Db::name('qt_user_menu_auth')->where('user_id',$user_id)->delete();
            }
            $data = [];
            foreach ($role_menu_data as $key => $val){
                $data[] = ['user_id'=>$user_id, 'menu_id'=>$val['id'],'platform'=>$val['platform']];
            }
            Db::name('qt_user_menu_auth')->insertAll($data);
        }
    }


    /**
     * 更新指定员工的权限菜单
     */
    public static function updateUserMenuAuth($user_id,$menu_arr) {

        $menu_data = Db::name('qt_user_menu')->whereIn('id',$menu_arr)->select();
        $menu_name = array_column($menu_data,'title');

        $platform = in_array('电脑权限',$menu_name) ? 1 :2;
        Db::name('qt_user_menu_auth')->where('user_id',$user_id)->where('platform',$platform)->delete();

        $data = [];
        foreach ($menu_data as $key => $val){
            $data[] = ['user_id'=>$user_id, 'menu_id'=>$val['id'],'platform'=>$platform];
        }
        if (!empty($data)) {
            Db::name('qt_user_menu_auth')->insertAll($data);
        }
    }



    /**
     * 更新指定员工的权限菜单
     */
    public static function updateRoleMenu($role_id,$menu_arr) {

        $menu_data = Db::name('qt_user_menu')->whereIn('id',$menu_arr)->select();
        $menu_name = array_column($menu_data,'title');
        $platform = in_array('电脑权限',$menu_name) ? 1 :2;
        Db::name('qt_user_role_menu')->where('role_id',$role_id)->where('platform',$platform)->delete();

        $data = [];
        foreach ($menu_data as $key => $val){
            $data[] = ['role_id'=>$role_id, 'menu_id'=>$val['id'],'platform'=>$platform];
        }
        if (!empty($data)) {
            Db::name('qt_user_role_menu')->insertAll($data);
        }
    }


}